
import path from 'path';
import chalk from "chalk"; // 颜色
// @ts-ignore
import downloaded from "download-git-repo"; // 从 github 下载模板

export type DownloadReturnValue = {
  state: 1 | 2 ;
  name: string;
  dir: string;
  message: string
}
// 下载模版
export function downloadTemplate({ templateInfo, projectName }: any): Promise<DownloadReturnValue> | DownloadReturnValue {
  const dir = path.join(process.cwd(), projectName);
  return new Promise((resolve, reject) => {
    downloaded(templateInfo.url, dir, { clone: true }, (err: any) => {
      if (err) {
        // console.log('项目模版下载失败 =》error', chalk.red(err))
        return { state: 1, name: projectName, dir, message: '项目模版下载失败', }
      }
      resolve({ state: 2, name: projectName, dir, message: '项目模版下载成功', })
    })
  })
}
